//
//  WSAViewController.swift
//  iiatn
//
//  Created by mine on 18/4/26.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import Photos
import SVProgressHUD

class WSAViewController: UIViewController, UIActionSheetDelegate {
    
    
    @IBOutlet var head: UIButton!
    
    @IBOutlet var qrcode: UIButton!
    
    @IBOutlet var bottom: UIButton!
    
    @IBOutlet var bottom1: UIButton!

    @IBOutlet var share: UIButton!
    
    //存放照片资源的标志符
    var localId:String!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setstyle()
        dosth()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    func setstyle(){
        head.x = (SCREENW-head.width)/2
        qrcode.x = (SCREENW-qrcode.width)/2
        bottom.x = (SCREENW-bottom.width)/2
        bottom1.x = (SCREENW-bottom.width)/2
        share.x = (SCREENW-bottom.width)/2
    
        head.layer.cornerRadius = head.height * 0.06
        head.layer.masksToBounds = true
        head.layer.rasterizationScale = UIScreen.main.scale
        
        bottom.layer.cornerRadius = bottom.height * 0.06
        bottom.layer.masksToBounds = true
        bottom.layer.rasterizationScale = UIScreen.main.scale
        
        bottom1.layer.cornerRadius = bottom1.height * 0.06
        bottom1.layer.masksToBounds = true
        bottom1.layer.rasterizationScale = UIScreen.main.scale
        
        share.layer.cornerRadius = share.height * 0.06
        share.layer.masksToBounds = true
        share.layer.rasterizationScale = UIScreen.main.scale
        
    }
    
    func dosth(){
        setaction()
        share.addTarget(self, action: #selector(toshare), for: .touchUpInside)
    }
    
    func setaction(){
        let longpressGesutre = UILongPressGestureRecognizer(target: self, action: #selector(longpress))
        //let longpressGesutre = UILongPressGestureRecognizer(target: self, action: Selector(("handleLongpressGesture")))
        //长按时间为1秒
        longpressGesutre.minimumPressDuration = 1.2
        //允许15秒运动
        longpressGesutre.allowableMovement = 15
        //所需触摸1次
        longpressGesutre.numberOfTouchesRequired = 1
        self.view.addGestureRecognizer(longpressGesutre)
    }

    //长按手势
//    func handleLongpressGesture(sender : UILongPressGestureRecognizer){
//        if sender.state == UIGestureRecognizerState.began{
//            print(" long press !")
//        }
//    }
    
    func longpress(){
        let image = UIImage(named: "qrcode_140")
        PHPhotoLibrary.shared().performChanges({
            let result = PHAssetChangeRequest.creationRequestForAsset(from: image!)
            let assetPlaceholder = result.placeholderForCreatedAsset
            //保存标志符
            self.localId = assetPlaceholder?.localIdentifier
        }) { (isSuccess: Bool, error: Error?) in
            if isSuccess {
                print("保存成功!")
                //通过标志符获取对应的资源
                let assetResult = PHAsset.fetchAssets(
                    withLocalIdentifiers: [self.localId], options: nil)
                let asset = assetResult[0]
                let options = PHContentEditingInputRequestOptions()
                options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData)
                    -> Bool in
                    return true
                }
                //获取保存的图片路径
                asset.requestContentEditingInput(with: options, completionHandler: {
                    (contentEditingInput:PHContentEditingInput?, info: [AnyHashable : Any]) in
                    print("地址：",contentEditingInput!.fullSizeImageURL!)
                    SVProgressHUD.showInfo(withStatus: "已成功保存到本地：\n\(contentEditingInput!.fullSizeImageURL!)")
                })
            } else{
                print("保存失败：", error!.localizedDescription)
            }
        }
    }
    func toshare(){
        share(sdkType:SSDKPlatformType.typeWechat)
    }
    
    func share(sdkType: SSDKPlatformType){
        // 1.创建分享参数
        
        let shareParames = NSMutableDictionary()
        shareParames.ssdkSetupShareParams(byText: "国投网AndroidAPP",
                                          images : UIImage(named:"qrcode_140"),
                                          url : NSURL(string: BASE_URL ) as URL!,
                                          title : "国投网AndroidAPP分享",
                                          type : SSDKContentType.image)
        
        //2.进行分享 SSDKPlatformType.typeSinaWeibo
        ShareSDK.share(sdkType, parameters: shareParames) { (state : SSDKResponseState, nil, entity : SSDKContentEntity?, error :Error?) in
            
            switch state{
                
            case SSDKResponseState.success: print("分享成功")
            case SSDKResponseState.fail:    print("授权失败,错误描述:\(error)")
            case SSDKResponseState.cancel:  print("操作取消")
                
            default:
                break
            }
            
        }
    }
}
